import XEUtils from "xe-utils";
import moment from "moment";
import GlobalConfig from "../../conf";
import formats from "../../v-c-u-table/src/formats";
import { Modal } from "vcu";

let zindexIndex = 0;
let lastZindex = 1;

function getColFuncWidth(isExists, defaultWidth = 16) {
  return isExists ? defaultWidth : 0;
}

class ColumnInfo {
  /* eslint-disable @typescript-eslint/no-use-before-define */
  constructor($xetable, _vm, { renderHeader, renderCell, renderFooter, renderData } = {}) {
    const $xegrid = $xetable.$xegrid;
    const proxyOpts = $xegrid ? $xegrid.proxyOpts : null;
    const formatter = _vm.formatter;
    const visible = XEUtils.isBoolean(_vm.visible) ? _vm.visible : true;
    if (_vm.cellRender && _vm.editRender) {
      UtilTools.warn("vcu.error.errConflicts", ["column.cell-render", "column.edit-render"]);
    }
    if (_vm.type === "expand") {
      if ($xetable.treeConfig && $xetable.treeOpts.line) {
        UtilTools.error("vcu.error.errConflicts", ["tree-config.line", "column.type=expand"]);
      }
    }
    if (formatter) {
      if (XEUtils.isString(formatter)) {
        const globalFunc = formats.get(formatter) || XEUtils[formatter];
        if (!XEUtils.isFunction(globalFunc)) {
          UtilTools.error("vcu.error.notFunc", [formatter]);
        }
      } else if (XEUtils.isArray(formatter)) {
        const globalFunc = formats.get(formatter[0]) || XEUtils[formatter[0]];
        if (!XEUtils.isFunction(globalFunc)) {
          UtilTools.error("vcu.error.notFunc", [formatter[0]]);
        }
      }
    }
    Object.assign(this, {
      // 基本属性
      type: _vm.type,
      property: _vm.field,
      title: _vm.title,
      width: _vm.width,
      minWidth: _vm.minWidth,
      resizable: _vm.resizable,
      fixed: _vm.fixed,
      align: _vm.align,
      headerAlign: _vm.headerAlign,
      footerAlign: _vm.footerAlign,
      showOverflow: _vm.showOverflow,
      showHeaderOverflow: _vm.showHeaderOverflow,
      showFooterOverflow: _vm.showFooterOverflow,
      className: _vm.className,
      headerClassName: _vm.headerClassName,
      footerClassName: _vm.footerClassName,
      formatter: formatter,
      sortable: _vm.sortable,
      sortBy: _vm.sortBy,
      sortMethod: _vm.sortMethod,
      remoteSort: _vm.remoteSort,
      filters: UtilTools.getFilters(_vm.filters),
      filterMultiple: XEUtils.isBoolean(_vm.filterMultiple) ? _vm.filterMultiple : true,
      filterMethod: _vm.filterMethod,
      filterRender: _vm.filterRender,
      treeNode: _vm.treeNode,
      cellType: _vm.cellType,
      cellRender: _vm.cellRender,
      editRender: _vm.editRender,
      contentRender: _vm.contentRender,
      exportMethod: _vm.exportMethod,
      footerExportMethod: _vm.footerExportMethod,
      titleHelp: _vm.titleHelp,
      // 自定义参数
      params: _vm.params,
      // 渲染属性
      id: _vm.colId || XEUtils.uniqueId("col_"),
      parentId: null,
      visible,
      halfVisible: false,
      defaultVisible: visible,
      checked: false,
      halfChecked: false,
      disabled: false,
      level: 1,
      rowSpan: 1,
      colSpan: 1,
      order: null,
      renderWidth: 0,
      renderHeight: 0,
      resizeWidth: 0,
      renderLeft: 0,
      renderArgs: [], // 渲染参数可用于扩展
      model: {},
      renderHeader: renderHeader || _vm.renderHeader,
      renderCell: renderCell || _vm.renderCell,
      renderFooter: renderFooter || _vm.renderFooter,
      renderData: renderData,
      // 单元格插槽，只对 grid 有效
      slots: _vm.slots,
      //通用平台
      key: _vm.key ? _vm.key : "",
      ora_name: _vm.ora_name ? _vm.ora_name : "",
      filter_name: _vm.filter_name ? _vm.filter_name : "",
      disabledCheckboxColumn: _vm.disabledCheckboxColumn ? _vm.disabledCheckboxColumn : false,
      headerLoadRes: _vm.headerLoadRes,
    });
    if (proxyOpts && proxyOpts.beforeColumn) {
      proxyOpts.beforeColumn({ $grid: $xegrid, column: this });
    }
  }

  getTitle() {
    let seqTitle = GlobalConfig.table.seqTitle ? GlobalConfig.table.seqTitle : GlobalConfig.i18n("vcu.table.seqTitle");
    return UtilTools.getFuncText(this.title || (this.type === "seq" ? seqTitle : ""));
  }

  getKey() {
    return this.property || (this.type ? `type=${this.type}` : null);
  }

  update(name, value) {
    // 不支持双向的属性
    if (name !== "filters") {
      if (name === "field") {
        this.property = value;
      } else {
        this[name] = value;
      }
    }
  }
}

function outLog(type) {
  return function (message, params) {
    const msg = UtilTools.getLog(message, params);
    console[type](msg);
    return msg;
  };
}

export const UtilTools = {
  warn: outLog("warn"),
  error: outLog("error"),
  getLog(message, args) {
    return `[vcu-table] ${XEUtils.template(GlobalConfig.i18n(message), args)} `;
  },
  getFuncText(content) {
    return XEUtils.isFunction(content) ? content() : GlobalConfig.translate ? GlobalConfig.translate(content) : content;
  },
  nextZIndex() {
    lastZindex = GlobalConfig.zIndex + zindexIndex++;
    return lastZindex;
  },
  getLastZIndex() {
    return lastZindex;
  },
  // 行主键 key
  getRowkey($xetable) {
    return $xetable.rowId || "_XID";
  },
  // 行主键 value
  getRowid($xetable, row) {
    const rowId = XEUtils.get(row, UtilTools.getRowkey($xetable));
    return rowId ? encodeURIComponent(rowId) : "";
  },
  // 获取所有的列，排除分组
  getColumnList(columns) {
    const result = [];
    columns.forEach((column) => {
      result.push(...(column.children && column.children.length ? UtilTools.getColumnList(column.children) : [column]));
    });
    return result;
  },
  isEnableConf(conf) {
    return conf && conf.enabled !== false;
  },
  getClass(property, params) {
    return property ? (XEUtils.isFunction(property) ? property(params) : property) : "";
  },
  getFilters(filters) {
    if (filters && XEUtils.isArray(filters)) {
      return filters.map(({ label, value, data, resetValue, checked }) => {
        return { label, value, data, resetValue, checked: !!checked, _checked: !!checked };
      });
    }
    return filters;
  },
  formatText(value, placeholder) {
    return "" + (value === "" || value === null || value === undefined ? (placeholder ? GlobalConfig.emptyCell : "") : value);
  },
  getCellValue(row, column) {
    return XEUtils.get(row, column.property);
  },
  setCellValue(row, column, value) {
    return XEUtils.set(row, column.property, value);
  },
  isColumn(column) {
    return column instanceof ColumnInfo;
  },
  getColumnConfig($xetable, _vm, options) {
    return UtilTools.isColumn(_vm) ? _vm : new ColumnInfo($xetable, _vm, options);
  },
  // 组装列配置
  assemColumn(_vm) {
    const { $el, $xetable, $xecolumn, columnConfig } = _vm;
    const groupConfig = $xecolumn ? $xecolumn.columnConfig : null;
    columnConfig.slots = _vm.$scopedSlots;
    if (groupConfig) {
      if (!groupConfig.children) {
        groupConfig.children = [];
      }
      groupConfig.children.splice([].indexOf.call($xecolumn.$el.children, $el), 0, columnConfig);
    } else {
      $xetable.staticColumns.splice([].indexOf.call($xetable.$refs.hideColumn.children, $el), 0, columnConfig);
    }
  },
  // 销毁列
  destroyColumn(_vm) {
    const { $xetable, columnConfig } = _vm;
    const matchObj = XEUtils.findTree($xetable.staticColumns, (column) => column === columnConfig);
    if (matchObj) {
      matchObj.items.splice(matchObj.index, 1);
    }
  },
  hasChildrenList(item) {
    return item && item.children && item.children.length > 0;
  },
  getColMinWidth(_vm, column) {
    const { sortOpts, filterOpts, editOpts } = _vm;
    const { type, filters, sortable, remoteSort, editRender, titleHelp } = column;
    return (
      40 +
      getColFuncWidth(type === "checkbox", 18) +
      getColFuncWidth(titleHelp, 18) +
      getColFuncWidth(filters && filterOpts.showIcon) +
      getColFuncWidth((sortable || remoteSort) && sortOpts.showIcon) +
      getColFuncWidth(editRender && editOpts.showIcon, 32)
    );
  },
  parseFile(file) {
    const name = file.name;
    const tIndex = XEUtils.lastIndexOf(name, ".");
    const type = name.substring(tIndex + 1, name.length);
    const filename = name.substring(0, tIndex);
    return { filename, type };
  },
  //弹出提示
  showTipsModal(data) {
    try {
      return new Promise((resolve, reject) => {
        Modal.info({
          mask: false,
          title: "提示",
          class: data.className ? data.className : "",
          autoFocusButton: null,
          centered: true,
          content: (h) => {
            return h("div", {
              domProps: {
                innerHTML: data.content ? data.content : "",
              },
            });
          },
          okText: "确定",
          onOk() {
            resolve(true);
          },
        });
      });
    } catch (err) {
      return false;
    }
  },
  //弹出确认提示
  showConfirmModal(data) {
    try {
      return new Promise((resolve, reject) => {
        Modal.confirm({
          mask: false,
          title: "提示",
          class: data.className ? data.className : "",
          autoFocusButton: null,
          centered: true,
          content: (h) => {
            return h("div", {
              domProps: {
                innerHTML: data.content ? data.content : "",
              },
            });
          },
          okText: "确定",
          onOk() {
            resolve(true);
          },
          cancelText: "取消",
          onCancel() {
            resolve(false);
          },
        });
      });
    } catch (err) {
      return false;
    }
  },
  getmoment(val) {
    let obj = moment(val).toObject();
    if (obj.years != 1899) {
      if (obj.hours != 0) {
        return moment(val).format("YYYY-MM-DD hh:mm:ss");
      } else {
        return moment(val).format("YYYY-MM-DD");
      }
    } else {
      return moment(val).format("hh:mm:ss");
    }
  },
  removeScrollListener(scrollElem) {
    if (scrollElem && scrollElem._onscroll) {
      scrollElem.onscroll = null;
    }
  },
  restoreScrollListener(scrollElem) {
    if (scrollElem && scrollElem._onscroll) {
      scrollElem.onscroll = scrollElem._onscroll;
    }
  },
  eqEmptyValue(cellValue) {
    return cellValue === "" || XEUtils.eqNull(cellValue);
  },
};

export default UtilTools;
